package com.elong.businesstravel.modules.home.adapter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.List;

import com.android.support.jhf.utils.ToolUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.entitys.City;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

/**
 * 城市列表自动完成
 * 
 * @author lenovo
 * 
 */
public class CityAutoCompleteAdapter extends BaseAdapter implements Filterable {

	private Context mContext;
	private List<City> mCities;
	private List<City> mRawCities;
	private Filter mFilter;

	public CityAutoCompleteAdapter(Context context, List<City> cities, List<City> rawCities) {
		mContext = context;
		mCities = cities;
		mRawCities = rawCities;
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}

	public City getCityByIndex(int index) {
		return mCities.get(index);
	}

	/**
	 * Returns the context associated with this array adapter. The context is
	 * used to create views from the resource passed to the constructor.
	 * 
	 * @return The Context associated with this adapter.
	 */
	public Context getContext() {
		return mContext;
	}

	/**
	 * {@inheritDoc}
	 */
	public int getCount() {
		return mCities.size();
	}

	/**
	 * {@inheritDoc}
	 */
	public Object getItem(int position) {
		return mCities.get(position).toString();
	}

	/**
	 * {@inheritDoc}
	 */
	public long getItemId(int position) {
		return position;
	}

	/**
	 * {@inheritDoc}
	 */
	public View getView(int position, View convertView, ViewGroup parent) {

		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.view_simple_item, null);
		}

		TextView itemTextView = (TextView) convertView
				.findViewById(R.id.itemTextView);
		itemTextView.setText(mCities.get(position).cityName);

		return convertView;
	}

	/**
	 * {@inheritDoc}
	 */
	public Filter getFilter() {
		if (mFilter == null) {
			mFilter = new CityArrayFilter(this, mRawCities, mCities);
		}
		return mFilter;
	}

	/**
	 * <p>
	 * An array filter constrains the content of the array adapter with a
	 * prefix. Each item that does not start with the supplied prefix is removed
	 * from the list.
	 * </p>
	 */
	private static class CityArrayFilter extends Filter {
		
		private List<City> mRawCities;
		private List<City> mCities;
		private CityAutoCompleteAdapter mCityAutoCompleteAdapter;

		public CityArrayFilter(CityAutoCompleteAdapter cityAutoCompleteAdapter,
				List<City> rawCities, List<City> cities) {
			mRawCities = rawCities;
			mCities = cities;
			mCityAutoCompleteAdapter = cityAutoCompleteAdapter;
		}

		/**
		 * The method is called when receive softkeybord entry's char
		 */
		@Override
		protected FilterResults performFiltering(CharSequence prefix) {

			FilterResults filterResults = new FilterResults();

			List<City> tempCities = new ArrayList<City>();
			if (null != prefix) {
				if (!TextUtils.isEmpty(prefix.toString())) {
					for (int i = 0; i < mRawCities.size(); i++) {
						if (mRawCities.get(i).cityName.startsWith(prefix
								.toString())) {
							tempCities.add(mRawCities.get(i));
						} else if (mRawCities.get(i).allCode.startsWith(prefix
								.toString().toLowerCase())) {
							tempCities.add(mRawCities.get(i));
						}
					}
				}
			}

			filterResults.values = tempCities;
			filterResults.count = tempCities.size();

			return filterResults;
		}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			mCities.clear();
			List<City> valuesCities = (List<City>) results.values;
			if (null != valuesCities) {
				mCities.addAll(valuesCities);
			}
			if (results.count > 0) {
				mCityAutoCompleteAdapter.notifyDataSetChanged();
			} else {
				mCityAutoCompleteAdapter.notifyDataSetInvalidated();
			}
		}
	}
}
